package com.zq.learn.springbatch.quartz;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * QuartzTest
 *
 * @author ZhangQi
 * @date 2020/2/17
 */
public class QuartzTest {
  public static void main(String[] args) {

    try {
      //新建容器工厂，用于执行任务
      SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
      Scheduler sched = schedFact.getScheduler();
      sched.start();

      // 定义Job实例
      JobDetail job = newJob(HelloJob.class)
        .withIdentity("myJob", "group1")
        .build();

      // 定义Trigger实例，执行规则：一直循环执行，执行间隔10秒
      Trigger trigger = newTrigger()
        .withIdentity("myTrigger", "group1")
        .startNow()
        .withSchedule(simpleSchedule()
          .withIntervalInSeconds(10)
          .repeatForever())
        .build();

      // 绑定Job和Trigger
      sched.scheduleJob(job, trigger);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
